package de.swm.commons.mobile.client.presenter;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import de.swm.commons.mobile.client.constants.I18NConstants;
import de.swm.commons.mobile.client.utils.ISwmPopupUtil;
import de.swm.commons.mobile.client.utils.SwmPopupUtilImpl;
import de.swm.gwt.client.mobile.IPage;

/**
 *  Controlls the currently active presenter. Will handle all page leave events.
 *
 * @author wiese.daniel
 *         <br>
 *         copyright (C) 2012, Stadtwerke München GmbH
 */
public class PresenterController {

	private final static PresenterController INSTANCE = new PresenterController();
	private IMobilePresenter activePresenter;
	private IPage activeView;
	private ISwmPopupUtil swmPopupUtil = new SwmPopupUtilImpl();
	private final I18NConstants appConstants = GWT.create(I18NConstants.class);

	/**
	 * Private constructor - singelton
	 */
	private PresenterController() {
		//Utility constructor.
	}


	/**
	 * Will set the currently active presenter.
	 * @param activePresenter the persenter which is currenty active.
	 * @param activeView
	 */
	public void setActivePresenter(IMobilePresenter activePresenter, IPage activeView) {

		this.activePresenter = activePresenter;
		this.activeView = activeView;
	}

	/**
	 * Call this method if you want to leave a Page form another action then the back Action (e.g. Menu bar)
	 * @param result true if the controller of the current page allows the page leave action.
	 */
	public void beforePageLeave(AsyncCallback<Boolean> result) {
		if (activePresenter != null) {
			final LeavePageResponse leavePageResponse = activePresenter.leavePageRequest();
			switch (leavePageResponse) {
				case YES_LEAVE_PAGE:
					result.onSuccess(true);
					break;
				case NO_STAY_ON_CURRENT_PAGE:
					result.onSuccess(false);
					break;
				case DISPLAY_YES_NO_DIALOG:
					displayYesNoDialog(result);
			}
		} else {
			result.onSuccess(true);
		}
	}

	private void displayYesNoDialog(final AsyncCallback<Boolean> result) {
		ClickHandler okClickHanlder = new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (event != null) {
					event.stopPropagation();
				}
				result.onSuccess(true);
			}
		};
		ClickHandler cancelClickHanlder = new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (event != null) {
					event.stopPropagation();
				}
				result.onSuccess(false);
			}
		};
		swmPopupUtil.showPopupYesNo(true, appConstants.leavePageQuestionHeader(), appConstants.leavePageQuestionText(), okClickHanlder, cancelClickHanlder);
	}


	public static PresenterController get() {
		return INSTANCE;
	}


	public IPage getActiveView() {
		return activeView;
	}

	public IMobilePresenter getActivePresenter() {
		return activePresenter;
	}
}
